package top.z.orm.handle.osql.dialect;

import top.z.orm.core.osql.dialect.DbFeature;
import top.z.orm.handle.osql.core.HoneyUtil;

import static top.z.orm.core.osql.SqlKeyWord.*;

public class LimitOffsetPaging implements DbFeature {

    @Override
    public String toPageSql(String sql, int offset, int size) {
        if (HoneyUtil.isRegPagePlaceholder()) {
            int[] array = new int[2];
            array[0] = size;
            array[1] = offset;
            HoneyUtil.regPageNumArray(array);
            // return sql + " limit ? offset ?";
            return sql + SPACE + LIMIT + SPACE + QUESTION + OFFSET + QUESTION + SPACE;
        } else {
            // return sql + " limit " + size + " offset " + offset;
            return sql + SPACE + LIMIT + SPACE + size + SPACE + OFFSET + SPACE + offset;
        }
    }

    @Override
    public String toPageSql(String sql, int size) {
        if (HoneyUtil.isRegPagePlaceholder()) {
            int[] array = new int[1];
            array[0] = size;
            HoneyUtil.regPageNumArray(array);
            // sql + " limit ?"
            return sql + SPACE + LIMIT + SPACE + QUESTION;
        } else {
            return sql + SPACE + LIMIT + SPACE + size;
        }
    }
}
